package cn.school.controller.job;


import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.job.JobDetails;
import cn.school.job.QuartzManager;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Api(tags = "定时任务管理")
@RequestMapping("/job")
public class JobController {

    @Autowired
    private QuartzManager qtzManager;

    @SuppressWarnings("unchecked")
    private static Class<? extends QuartzJobBean> getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (Class<? extends QuartzJobBean>) class1;
    }

    /**
     * 添加定时任务
     * @throws Exception
     */
    @PostMapping(value = "/addjob")
    public CommonResult<Integer> addjob(@RequestBody Map<String,String> paramMap) throws Exception {
        String jobClassName = paramMap.get("jobClassName");
        String jobGroupName = paramMap.get("jobGroupName");
        String jobName = paramMap.get("jobName");
        String cronExpression = paramMap.get("cronExpression");
        qtzManager.addOrUpdateJob(getClass(jobClassName), jobName, jobGroupName, cronExpression);
        return CommonResult.success();
    }

    /**
     * 暂停定时任务
     * @throws Exception
     */
    @PostMapping(value = "/pausejob")
    public CommonResult<Integer> pausejob(@RequestBody Map<String,String> paramMap) throws Exception {
        String jobClassName = paramMap.get("jobClassName");
        String jobGroupName = paramMap.get("jobGroupName");
        qtzManager.pauseJob(jobClassName, jobGroupName);
        return CommonResult.success();
    }

    /** 恢复定时任务
     * @throws Exception
     */
    @PostMapping(value = "/resumejob")
    public  CommonResult<Integer> resumejob(@RequestBody Map<String,String> paramMap) throws Exception {
        String jobClassName = paramMap.get("jobClassName");
        String jobGroupName = paramMap.get("jobGroupName");
        qtzManager.resumeJob(jobClassName, jobGroupName);
        return CommonResult.success();
    }

    /** 更新定时任务
     * @throws Exception
     */
    @PostMapping(value = "/reschedulejob")
    public void rescheduleJob(@RequestBody Map<String,String> paramMap) throws Exception {
        String jobClassName = paramMap.get("jobClassName");
        String jobGroupName = paramMap.get("jobGroupName");
        String cronExpression = paramMap.get("cronExpression");
        qtzManager.addOrUpdateJob(getClass(jobClassName), jobClassName, jobGroupName, cronExpression);
    }

    /** 删除定时任务
     * @throws Exception
     */
    @PostMapping(value = "/deletejob")
    public CommonResult<Integer> deletejob(@RequestBody Map<String,String> paramMap) throws Exception {
        String jobClassName = paramMap.get("jobClassName");
        String jobGroupName = paramMap.get("jobGroupName");
        qtzManager.deleteJob(jobClassName, jobGroupName);
        return CommonResult.success();
    }

    /** 查询定时任务
     * @return
     */
    @PostMapping(value = "/queryjob")
    public CommonResult<CommonPage<JobDetails>> queryjob(@RequestBody Map<String,Object> paramMap) {
        String pageNum = String.valueOf(paramMap.get("pageNum"));
        String pageSize = String.valueOf(paramMap.get("pageSize"));
        IPage<JobDetails> jobAndTrigger = qtzManager.queryAllJobBean(Integer.valueOf(pageNum), Integer.valueOf(pageSize));

        return CommonResult.success(CommonPage.restPage(jobAndTrigger));
    }
}
